package savehalloween;

import java.util.Vector;

import javax.microedition.lcdui.Graphics;

public class Grid {
	private int height = 11;
	private int width = 10;
	private Vector towerList;
	
	private Tower[][] towers;
		
	public Grid(){
		towers = new Tower[width][height];
		towerList = new Vector();
		
		for(int i = 0; i < width; i++){
			for(int j = 0; j < height; j++){
				towers[i][j] = null;
			}
		}
	}
	
	public void removeTowerAt(int x, int y){
		towers[x][y] = null;
	}
	
	public boolean isFieldFree(int x, int y){
		if(towers[x][y] == null){
			return true;
		}
		return false;
	}
	
	public void addTowerAt(Tower tower, int posX, int posY){
		towers[posX][posY] = tower;
		towerList.addElement(tower);
		tower.setX(posX).setY(posY);
	}
	
	public void removeTower(Tower tower){
		for(int i = 0; i < width; i++){
			for(int j = 0; j < height; j++){
				if(towers[i][j] == tower){
					towers[i][j] = null;
					towerList.removeElement(tower);
					return;
				}
			}
		}
		return;
	}
	
	public String toString(){
		String output = "";
		for(int i = 0; i < width; i++){
			for(int j = 0; j < height; j++){
				if(towers[i][j] == null){
					output += "- ";
				}else{
					output += towers[i][j] + " ";
				}
			}
			output += "\n";
		}
		return output;
	}
	
	public void paint(Graphics g){
		g.setColor(0xd3f4b3);
    	
    	for(int j = 0; j < 11; j++){
			g.fillRect(0, j*30, width*30, 2);
		}
    	
    	for(int i = 0; i < 10; i++){
    		g.fillRect(i*30, 0, 2, 330);
    	}
    	
    	for(int i = 0; i < towerList.size(); i++){
    		Tower my = (Tower)towerList.elementAt(i);
    		my.paint(g);
    	}
	}
}
